var ip = returnCitySN["cip"];
var address = returnCitySN["cname"];
let loadIcon;

layui.config({
    base: path + '/layuiadmin/' //静态资源所在路径
}).extend({
    index: 'lib/index' //主入口模块
}).use(['index', 'user'], function () {
    var $ = layui.$,
        form = layui.form

    form.render();

    var formMain = $("#formMain");
    var identification = $("#identification");
    var a_wechat = $("#a_wechat");
    var a_phone = $("#a_phone");
    var a_email = $("#a_email");
    var a_user = $(".a_user");
    var item1 = $("#item1");
    var item2 = $("#item2");

    function countdown(obj, seconds, text) {
        var that = $(obj);
        that.attr("disabled", true);
        that.html(seconds + '秒');
        let promise = new Promise((resolve, reject) => {
            let setTimer = setInterval(
                () => {
                    seconds -= 1;
                    // console.info('倒计时:' + seconds);
                    that.html(seconds + '秒');
                    if (seconds <= 0) {
                        that.html(text);
                        resolve(setTimer)
                    }
                }
                , 1000)
        })
        promise.then((setTimer) => {
            // console.info('清除');
            clearInterval(setTimer);
            that.attr("disabled", false);
        })
    }

    /*Phone*/
    a_phone.on("click", function () {
        $(formMain).html(phoneLoginHtml);
        /*发送短信验证码 start*/
        $("#getPhoneYzm").on("click", function () {
            var phone = $("#phone").val();
            if (isNull(phone)) {
                layer.msg("手机号码还未填写呢~", {
                    icon: 0,
                    time: 2000
                });
                $("#phone").focus();
                return;
            }
            $("#phoneTCaptcha").click();
        });
        /*发送短信验证码 end*/
        $(identification).val("phoneLogin");
        $(item1).show();
        $(item2).hide();
    });

    /*Email*/
    a_email.on("click", function () {
        $(formMain).html(emailLoginHtml);
        /*发送邮箱验证码 start*/
        $("#getEmailYzm").on("click", function () {
            var email = $("#email").val();
            if (isNull(email)) {
                layer.msg("请先填写邮箱账户哦~", {
                    icon: 0,
                    time: 2000
                });
                $("#email").focus();
                return;
            }
            $("#emailTCaptcha").click();
        });
        /*发送邮箱验证码 end*/
        $(identification).val("emailLogin");
        $(item1).show();
        $(item2).hide();
    });

    /*User*/
    a_user.on("click", function () {
        $(formMain).html(userLoginHtml);
        $(identification).val("userNameLogin");
        $(item1).hide();
        $(item2).show();
    });

    a_wechat.on("click", function () {
        window.location.href = path + "/weCatLogin.html";
    });

    /*开启遮罩*/
    function startLoad() {
        loadIcon = layer.load(0, {
            shade: true,
            time: false,
            shade: [0.1, '#fff']
        });
    }

    /*关闭遮罩*/
    function stopLoad() {
        layer.close(loadIcon);
    }


    /*邮箱发送人机验证*/
    window.emailCallback = function (res) {
        if (res.ret === 0) {
            var email = $("#email").val();
            startLoad();
            $.ajax({
                url: path + "/sendEmail.json"
                , type: "POST"
                , dataType: "JSON"
                , data: {
                    "email": email
                }
                , success: function (res) {
                    stopLoad();
                    if (res.code == "1") {
                        countdown($("#getEmailYzm"), 120, "重新获取");
                        layer.msg(res.msg, {
                            icon: 1,
                            time: 2000
                        });
                    } else {
                        layer.msg(res.msg, {
                            icon: 0,
                            time: 2000
                        });
                    }
                }, error: function () {
                    stopLoad();
                    layer.msg("邮件接口出现异常,请联系管理员！", {
                        icon: 2,
                        time: 2000
                    });
                }
            });
        }
    }
    new TencentCaptcha(document.getElementById('emailTCaptcha'));

    /*短信发送人机验证*/
    window.phoneCallback = function (res) {
        if (res.ret === 0) {
            var phone = $("#phone").val();
            startLoad();
            $.ajax({
                url: path + "/sendSMS.json"
                , type: "POST"
                , dataType: "JSON"
                , data: {
                    "phone": phone
                }
                , success: function (res) {
                    stopLoad();
                    if (res.code == "1") {
                        countdown($("#getPhoneYzm"), 60, "重新获取");
                        layer.msg(res.msg, {
                            icon: 1,
                            time: 2000
                        });
                    } else {
                        layer.msg(res.msg, {
                            icon: 0,
                            time: 2000
                        });
                    }
                }, error: function () {
                    stopLoad();
                    layer.msg("短信接口出现异常,请联系管理员！", {
                        icon: 2,
                        time: 2000
                    });
                }
            });
        }
    }
    new TencentCaptcha(document.getElementById('phoneTCaptcha'));

    /*登录人机验证*/
    window.submitCallback = function (res) {
        if (res.ret === 0) {
            switch ($(identification).val()) {
                case "userNameLogin":
                    var username = $("#username").val();
                    var password = $("#password").val();
                    var remember = $(".layui-form-checked").length == 1 ? true : false;
                    startLoad();
                    $.ajax({
                        url: path + "/doLogin.json"
                        , type: "POST"
                        , dataType: "JSON"
                        , data: {
                            "userName": username,
                            "passWord": password,
                            "remember": remember,
                            "ip": ip,
                            "address": address,
                            "loginType": "用户名登录"
                        }
                        , success: function (res) {
                            stopLoad();
                            if (res.code == "0") {
                                layer.msg(res.msg, {
                                    icon: 2,
                                    time: 2000
                                });
                            } else {
                                layer.msg(res.msg, {
                                    offset: '9px'
                                    , icon: 1
                                    , time: 1000
                                }, function () {
                                    location.href = path + "/backstage/home.html";
                                });
                            }
                        }, error: function () {
                            stopLoad();
                            layer.msg("认证接口出现异常,请联系管理员！", {
                                icon: 2,
                                time: 2000
                            });
                        }
                    });
                    break;
                case "phoneLogin":
                    var phone = $("#phone").val();
                    var phoneYzm = $("#phoneYzm").val();
                    startLoad();
                    $.ajax({
                        url: path + "/doPhoneLogin.json"
                        , type: "POST"
                        , dataType: "JSON"
                        , data: {
                            "phone": phone,
                            "phoneYzm": phoneYzm,
                            "ip": ip,
                            "address": address,
                            "loginType": "短信验证登录"
                        }
                        , success: function (res) {
                            stopLoad();
                            if (res.code == "0") {
                                layer.msg(res.msg, {
                                    icon: 2,
                                    time: 2000
                                });
                            } else {
                                layer.msg(res.msg, {
                                    offset: '9px'
                                    , icon: 1
                                    , time: 1000
                                }, function () {
                                    location.href = path + "/backstage/home.html";
                                });
                            }
                        }, error: function () {
                            stopLoad();
                            layer.msg("短信认证接口出现异常,请联系管理员！", {
                                icon: 2,
                                time: 2000
                            });
                        }
                    });
                    break;
                case "emailLogin":
                    var email = $("#email").val();
                    var emailYzm = $("#emailYzm").val();
                    startLoad();
                    $.ajax({
                        url: path + "/doEmailLogin.json"
                        , type: "POST"
                        , dataType: "JSON"
                        , data: {
                            "email": email,
                            "emailYzm": emailYzm,
                            "ip": ip,
                            "address": address,
                            "loginType": "邮箱验证登录"
                        }
                        , success: function (res) {
                            stopLoad();
                            if (res.code == "0") {
                                layer.msg(res.msg, {
                                    icon: 2,
                                    time: 2000
                                });
                            } else {
                                layer.msg(res.msg, {
                                    offset: '9px'
                                    , icon: 1
                                    , time: 1000
                                }, function () {
                                    location.href = path + "/backstage/home.html";
                                });
                            }
                        }, error: function () {
                            stopLoad();
                            layer.msg("邮件认证接口出现异常,请联系管理员！", {
                                icon: 2,
                                time: 2000
                            });
                        }
                    });
                    break;
            }
        }
    }
    new TencentCaptcha(document.getElementById('submitTCaptcha'));

    $("[lay-filter=picture-submit]").on("click", function () {

        switch ($(identification).val()) {
            case "userNameLogin":
                var username = $("#username").val();
                var password = $("#password").val();
                if (isNull(username, password)) {
                    return;
                }
                break;
            case "phoneLogin":
                var phone = $("#phone").val();
                var phoneYzm = $("#phoneYzm").val();
                if (isNull(phone, phoneYzm)) {
                    return;
                }
                break;
            case "emailLogin":
                var email = $("#email").val();
                var emailYzm = $("#emailYzm").val();
                if (isNull(email, emailYzm)) {
                    return;
                }
                break;
        }
        $("#submitTCaptcha").click();
    });

    /*微信登录*/
    if (openCode!=""){
        $.ajax({
            url: path + "/doWeCatLogin.json"
            , type: "POST"
            , dataType: "JSON"
            , data: {
                "ip": ip,
                "address": address,
                "loginType": "微信登录"
    }
            , success: function (res) {
                if (res.code == "0") {
                    layer.msg(res.msg, {
                        icon: 2,
                        time: 2000
                    });
                } else {
                    layer.msg(res.msg, {
                        offset: '9px'
                        , icon: 1
                        , time: 1000
                    }, function () {
                        location.href = path + "/backstage/home.html";
                    });
                }
            }, error: function () {
                layer.msg("微信登录接口出现异常,请联系管理员！", {
                    icon: 2,
                    time: 2000
                });
            }
        });
    }

});

